// Ext初始化函数
Ext.onReady(function() {

	Ext.QuickTips.init();

	var cookies = new Ext.state.CookieProvider();

	//"新建分析"目录树开始
	var AnalysisTreeRoot = new Ext.tree.TreeNode({
				id : "AnalysisRoot"
			});

	AnalysisTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "AddComprehensiveAnalysis",
				text : "新建信息管理",
				icon : "static/images/plus.png"
			}));

	AnalysisTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "AddThanAnalysis",
				text : "新建占比分析",
				icon : "static/images/folder-open-table.png"
			}));

	AnalysisTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "modSome",
				text : "新建高发分析",
				icon : "static/images/application_view_list.png"
			}));

	// "新建分析"目录树结束

	// "我的分析"目录树开始
	var MyAnalysis = new Ext.tree.TreeNode({
				id : "MyAnalysisRoot",
				text : "Demo"
			});
	MyAnalysis.appendChild(new Ext.tree.TreeNode({
				id : "CommunityAnalysis",
				text : "社区分析",
				icon : "static/images/information.png"
			}));

	MyAnalysis.appendChild(new Ext.tree.TreeNode({
				id : "GridingAnalysis",
				text : "网格分析",
				icon : "static/images/table_refresh.png"
			}));
	MyAnalysis.appendChild(new Ext.tree.TreeNode({
				id : "ProblemTimeBill",
				text : "问题时间统计",
				icon : "static/images/scripts-text.png"
			}));
	MyAnalysis.appendChild(new Ext.tree.TreeNode({
				id : "ProblemCategroyBill",
				text : "问题类别统计",
				icon : "static/images/chart.png"
			}));

	// "我的分析"目录树结束

	// "信息管理"目录树开始
	var ComprehensiveAnalysisTreeRoot = new Ext.tree.TreeNode({
				id : "ComprehensiveAnalysisRoot",
				text : "Demo"
			});
	ComprehensiveAnalysisTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "mgrProblem",
				text : "问题信息管理",
				icon : "static/images/calendar-list.png"
			}));
	ComprehensiveAnalysisTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "mgrCase",
				text : "案件信息管理",
				icon : "static/images/calendar-list.png"
			}));

	// "信息管理"目录树结束

	// "系统功能"目录树开始
	var SystemFunctionTreeRoot = new Ext.tree.TreeNode({
				id : "SysRoot",
				text : "Demo"
			});
	SystemFunctionTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "mgrUsers",
				text : "管理用户",
				icon : "static/images/user_edit.png"
			}));
	SystemFunctionTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "mgrArea",
				text : "地点信息管理",
				icon : "static/images/notebook--pencil.png"
			}));
	SystemFunctionTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "systemLoginLog",
				text : "系统登录日志",
				icon : "static/images/notebook--pencil.png"
			}));
	SystemFunctionTreeRoot.appendChild(new Ext.tree.TreeNode({
				id : "dbRecover",
				text : "数据库备份/恢复",
				icon : "static/images/databases.png"
			}));

	//"系统功能"目录树结束

	// north区域开始=================================================

	var cp3 = new Ext.state.CookieProvider();     
	var v=cp3.get('username'); 
	var north = new Ext.BoxComponent({
				region : 'north',
				margins : '10 10 5 5',
				height : 20, // give north and south regions a height
				autoEl : {
					tag : 'div',
					html : '<b>用户 ' +v+ ' ,欢迎您登陆！</b>'
				}
			});
	/*
	 * 
	 * 
	 * var north = new Ext.Panel({ region: "north",// 布局为north layout: "border",
	 * margins: '5 5 5 5', collapsible: true, title: "信息栏", height: 55, frame:
	 * true, items: [new Ext.BoxComponent({ region: 'north', //height: 35, //
	 * give north and south regions a height autoEl: { tag: 'div', html: '<b>用户 ' +
	 * cookies.get('username') + ' ,欢迎您登陆！</b>' } })]
	 * 
	 * });
	 */
	// north区域结束==================================================
	// south区域开始=====================================================
	/*
	 * var south = new Ext.Panel({ region: "south", height: 50, collapsible:
	 * true, margins: '5 5 5 5', title: " 底部面板 " });
	 */
	// south区域结束=============================================================
	// west区域开始==========================================================
	var west = new Ext.Panel({
		region : 'west',
		id : 'west-panel', // 看下面的 Ext.getCmp() 函数，伸缩使用
		title : '导航菜单',
		split : true,
		width : 200,
		minSize : 175,
		maxSize : 400,
		collapsible : true,
		margins : '0 0 5 5',
		layout : {
			type : 'accordion',
			animate : true
		},
		tbar : [{
					id : 'logoffsys',
					text : '注销',
					iconCls : 'static/images/connect.png',
					handler : function() {
						ClearCookie();
						window.location.href = 'login.jsp';
						// Ext.Msg.alert('系统注销了');
					}

				}, {
					id : 'exitsys',
					iconCls : 'static/images/exit.gif',
					text : '退出',
					handler : function() {
						ClearCookie();
						Ext.Msg.alert('消息','系统退出了');
						window.location.href = 'login.jsp';
					}
				}],

		items : [{
			contentEl : 'west',
			title : '新建分析',
			autoScroll : true,
			border : false,
			iconCls : 'settings',
			items : [new Ext.tree.TreePanel({
				root : AnalysisTreeRoot,
				rootVisible : false,
				border : false,

				listeners : {
					"click" : function(node, e) {
						e.stopEvent();
						var tab = center.getItem(node.id);
						if (!tab) {
							tab = center.add({
								id : node.id,
								iconCls : "static/images/add.gif",
								xtype : "panel",
								title : node.text,
								closable : true,
								html : "<iframe src=\'"
										+ node.id
										+ ".jsp\' frameborder='0' width='100%' height='100%'></iframe>"
							});

						}
						center.setActiveTab(tab);
					}

				}
			})]

		}, {
			title : '我的分析',
			border : false,
			iconCls : 'finance',

			items : [new Ext.tree.TreePanel({
				root : MyAnalysis,
				rootVisible : false,
				border : false,

				listeners : {
					"click" : function(node, e) {
						e.stopEvent();
						var tab = center.getItem(node.id);
						if (!tab) {
							tab = center.add({
								id : node.id,
								iconCls : "new-topic",
								xtype : "panel",
								title : node.text,
								closable : true,
								html : "<iframe src=\'"
										+ node.id
										+ ".jsp\' frameborder='0' width='100%' height='100%'></iframe>"
							});

						}
						center.setActiveTab(tab);
					}

				}
			})]
		}, {
			title : '信息管理',
			border : false,
			iconCls : 'book',
			items : [new Ext.tree.TreePanel({
				root : ComprehensiveAnalysisTreeRoot,
				rootVisible : false,
				border : false,

				listeners : {
					"click" : function(node, e) {
						e.stopEvent();
						var tab = center.getItem(node.id);
						if (!tab) {
							tab = center.add({
								id : node.id,
								iconCls : "new-topic",
								xtype : "panel",
								title : node.text,
								closable : true,
								html : "<iframe src=\'"
										+ node.id
										+ ".jsp\' frameborder='0' width='100%' height='100%'></iframe>"
							});

						}
						center.setActiveTab(tab);
					}

				}
			})]
		}, {
			title : '系统功能',
			border : false,
			iconCls : 'system',
			items : [new Ext.tree.TreePanel({
				root : SystemFunctionTreeRoot,
				rootVisible : false,
				border : false,

				listeners : {
					"click" : function(node, e) {
						e.stopEvent();
						var tab = center.getItem(node.id);
						if (!tab) {
							tab = center.add({
								id : node.id,
								iconCls : "new-topic",
								xtype : "panel",
								title : node.text,
								closable : true,
								html : "<iframe src=\'"
										+ node.id
										+ ".jsp\' frameborder='0' width='100%' height='100%'></iframe>"
							});

						}
						center.setActiveTab(tab);
					}

				}
					/*
				 * ,
				 * 
				 * listeners : { "click" : function(node, e) {
				 * e.stopEvent(); var tab = center.getItem(node.id);
				 * if (!tab) { tab = center.add({ id : node.id,
				 * iconCls : "new-topic", xtype : "panel", title :
				 * node.text, closable : true, html : "<iframe
				 * src=\'" + node.id + ".html\' frameborder='0'
				 * width='100%' height='100%'></iframe>" }); }
				 * center.setActiveTab(tab); } }
				 */
			})]
		}]
	});
	// west区域结束==============================================================

	// east区域开始==============================================================
	var east = new Ext.Panel({
				region : 'east',
				title : '属性窗格',
				collapsible : true,
				split : true,
				width : 225, // east的宽度
				minSize : 175,
				maxSize : 400,
				margins : '0 5 0 0',
				layout : 'fit', // items的layout manager
				items : // 这里是个总的Tab
				new Ext.TabPanel({
							border : false, // 总是展开
							activeTab : 1, // 第二个标签活跃
							tabPosition : 'bottom',
							items : [{
										html : '<p>测试.</p>',
										title : '标签1',
										autoScroll : true
									}, new Ext.grid.PropertyGrid({
												title : '属性',
												closable : true,
												source : {
													"(name)" : "Properties Grid",
													"排序" : false,
													"自动填充" : true,
													"产品质量" : false,
													"created" : new Date(Date
															.parse('10/15/2006')),
													"测试" : false,
													"版本" : 0.01,
													"宽度" : 1
												}
											})]
						})

			});
	// east区域结束==========================================================

	// 中央区域开始=============================================================
	var center = new Ext.TabPanel({
				region : 'center', // a center region is ALWAYS required for
				// border layout
				id : 'maincontent',
				margins : '0 5 5 0',
				title : '主区',
				deferredRender : false,
				activeTab : 0, // 第一个Tab激活
				items : [{
							contentEl : 'center1',// 对应下面的div ID
							title : '可关闭标签',
							closable : true,
							autoScroll : true
						}]
			});
	// 中央区域结束============================================================

	// 整体布局开始
	var vp = new Ext.Viewport({
				layout : "border",
				// items: [north, south, west, east, center]
				items : [north, west, center]
			});
		// 整体布局结束

		/*
	 * Ext.get("hideit").on('click', function() { // get a reference to the
	 * Panel that was created with id = // 'west-panel' var w =
	 * Ext.getCmp('west-panel'); // expand or collapse that Panel based on
	 * its collapsed property // state w.collapsed ? w.expand() :
	 * w.collapse(); });
	 */
});

// 清空Cookie的函数
function ClearCookie() {
	var cookies = new Ext.state.CookieProvider();
	cookies.set('username', '');
	cookies.set('role', '');
    
    
   Ext.get('styleswitcher_select').on('change',function(e,select){
        var name = select[select.selectedIndex].value;
        setActiveStyleSheet(name);
    });
    
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    Ext.get('styleswitcher_select').dom.value=title;
}